﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using System.Configuration;
using DevExpress.XtraEditors;
using ProgramMain.Framework;


namespace ProgramMain
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
        static private FrmMain m_MainForm = null;
        static public FrmMain MainForm
        {
            get { return m_MainForm; }
        }

		[STAThread]
		static void Main()
		{
            Application.SetCompatibleTextRenderingDefault(false);
            Process thisProc = Process.GetCurrentProcess();
		    thisProc.PriorityClass = ProcessPriorityClass.AboveNormal;

            ProgramMain.Skins.ApplicationSkins.EnableSkins();
            ProgramMain.Skins.ApplicationSkins.ResotreSkinSettings();
           
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");

            Framework.frmWaiting.ShowWaiting();
            if (Framework.GlobalData.OpenSqlConnection(Properties.Settings.Default.programConnectionString))
			{
                Framework.frmWaiting.HideWaiting();
                
                FrmLogin frmLogin = new FrmLogin();
				if (frmLogin.ShowDialog() == DialogResult.OK)
				{
                    frmLogin.Dispose();
				   if (BaseSecurity.CheckSecurityAccessType(BaseSecurity.UserAccessType.ManagerAccess) == BaseSecurity.SuperBool.True)
				   {
				       GlobalData.ChangeConnectionString("managerConnectionString");
				   }
				   else
				   {
                       GlobalData.ChangeConnectionString("techConnectionString");
				   }
                    m_MainForm = new FrmMain();
                    Application.Run(m_MainForm);

                    ProgramMain.Framework.GlobalData.SaveHistory();
				}
			}
			else
			{
                Framework.frmWaiting.HideWaiting();
                
                XtraMessageBox.Show("Не могу создать соединение с сервером базы данных MS SQL Server!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Stop);
			}

            ProgramMain.Framework.frmWaiting.StopWaitingDialogThread();
        }
	}
}